package vinh.game;

import utils.Constants;
import utils.Polygon;


public class Asteroid extends PolygonSprite{
	static final int 	MAX = 10;
	static final int    MIN_ASTEROIDS_SIDES =   4; // Ranges for this shape, size
  	static final int    MAX_ASTEROIDS_SIDES =  6; // speed and rotation.
  	static final int    MIN_ASTEROIDS_SIZE  =  20;
  	static final int    MAX_ASTEROIDS_SIZE  =  40;
  	static final double MIN_ASTEROIDS_SPEED = 1.0;//40.0 / Constants.FPS;
  	static final double MAX_ASTEROIDS_SPEED = 3.0;//240.0 / Constants.FPS;
  	static final double MAX_ASTEROIDS_SPIN  = Math.PI / Constants.FPS;
	
  	private double speed = Asteroid.MIN_ASTEROIDS_SPEED;
  	
	public Asteroid(){
		super();
	}
	
	public double speed(){
		return speed;
	}
	
	public void speed(double speed){
		this.speed = speed;
	}
	
	//initialize an asteroid, randomize the whole to have different asteroids at each initialization
	public void init(int width, int height){
		
		this.shape = new Polygon();
		
		int j;
		int s;
		double theta, r;
		int x, y;

		s = Asteroid.MIN_ASTEROIDS_SIDES + (int) (Math.random() * (Asteroid.MAX_ASTEROIDS_SIDES - Asteroid.MIN_ASTEROIDS_SIDES));

		for (j = 0; j < s; j++) {
			theta = 2 * Math.PI / s * j;
			r = Asteroid.MIN_ASTEROIDS_SIZE
					+ (int) (Math.random() * (Asteroid.MAX_ASTEROIDS_SIZE - Asteroid.MIN_ASTEROIDS_SIZE));
			x = (int) -Math.round(r * Math.sin(theta));
			y = (int) Math.round(r * Math.cos(theta));
			this.shape.addPoint(x,y);
		}
		
		this.angle = Math.random() * 2 * Math.PI;
		this.delta_angle = Math.random() * 2 * Asteroid.MAX_ASTEROIDS_SPIN - Asteroid.MAX_ASTEROIDS_SPIN;
		
		this.x = width/10 +(int) Math.round(Math.random() * 8 * width/10);
		this.y = height/10 + (int) Math.round(Math.random() * 8 * height/10);
		// Set a random motion for the this.

		this.delta_x = Math.random() * this.speed();
		if (Math.random() < 0.5)
			this.delta_x = -this.delta_x;
		this.delta_y = Math.random() * this.speed();
		if (Math.random() < 0.5)
			this.delta_y= -this.delta_y;
		active = true;
	}
	
	@Override
	public void advance(int width, int height, double speed){
		this.angle += this.delta_angle;
		if (this.angle < 0)
			this.angle += 2*Math.PI;
		if (this.angle > 2 * Math.PI)
			this.angle -= 2 * Math.PI;
	}

}
